﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CYTS.Aoyou.Framework.Log
{
    internal class CommonLogImpl : log4net.Core.LogImpl, ICommonLog
    {
        public CommonLogImpl(log4net.Core.ILogger logger)
            : base(logger)
        {
        }

        public override void Error(object message, Exception ex)
        {
            if (this.IsErrorEnabled)
            {
                log4net.Core.LoggingEvent loggingEvent = new log4net.Core.LoggingEvent(typeof(CommonLogImpl), Logger.Repository, Logger.Name, log4net.Core.Level.Error, message, ex);
                var context = CYTS.Aoyou.Framework.Context.ContextFactory.GetContext();
                if (context != null)
                {
                    loggingEvent.Properties["ServiceName"] = context.ServiceName;
                    loggingEvent.Properties["ServerName"] = context.ServerName;
                }
                Logger.Log(loggingEvent);
            }
        }

        public override void Info(object message)
        {
            if (this.IsInfoEnabled)
            {
                log4net.Core.LoggingEvent loggingEvent = new log4net.Core.LoggingEvent(typeof(CommonLogImpl), Logger.Repository, Logger.Name, log4net.Core.Level.Info, message, null);
                var context = CYTS.Aoyou.Framework.Context.ContextFactory.GetContext();
                if (context != null)
                {
                    loggingEvent.Properties["ServiceName"] = context.ServiceName;
                    loggingEvent.Properties["ServerName"] = context.ServerName;
                }
                Logger.Log(loggingEvent);
            }
        }
    }
}
